package com.one.square.user.server.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.square.core.domain.Result;
import com.one.square.core.entity.SysDictType;
import com.one.square.core.utils.JwtUtils;
import com.one.square.core.vo.QuerySysDictTypeVo;
import com.one.square.user.server.mapper.SysDictTypeMapper;
import com.one.square.user.server.service.SysDictTypeService;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author cwm
 * @Description
 * @date 2021/11/1 上午10:48
 * @Version 1.0
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
    @Override
    public Result<IPage> list(Page<SysDictType> page, QuerySysDictTypeVo querySysDictTypeVo) {
        LambdaQueryWrapper<SysDictType> wrapper = Wrappers.<SysDictType>lambdaQuery()
                .eq(StrUtil.isNotEmpty(querySysDictTypeVo.getDictName()), SysDictType::getDictName, querySysDictTypeVo.getDictName())
                .eq(StrUtil.isNotEmpty(querySysDictTypeVo.getDictType()), SysDictType::getDictType, querySysDictTypeVo.getDictType())
                .eq(Objects.nonNull(querySysDictTypeVo.getStatus()), SysDictType::getStatus, querySysDictTypeVo.getStatus())
                .ge(Objects.nonNull(querySysDictTypeVo.getStartTime()), SysDictType::getCreateTime, querySysDictTypeVo.getCreateTime())
                .le(Objects.nonNull(querySysDictTypeVo.getEndTime()), SysDictType::getCreateTime, querySysDictTypeVo.getEndTime())
                .orderByDesc(SysDictType::getCreateTime);
        return Result.succeed(this.baseMapper.selectPage(page, wrapper));
    }

    @Override
    public void add(SysDictType sysDictType) {
        sysDictType.setCreateBy(JwtUtils.getUserId());
        this.baseMapper.insert(sysDictType);
    }

    @Override
    public void update(SysDictType sysDictType) {
        sysDictType.setUpdateBy(JwtUtils.getUserId());
        this.baseMapper.updateById(sysDictType);
    }
}
